/**
 * 
 */
package top.lcpsky.flashcards.util;

import com.fasterxml.jackson.databind.ObjectMapper;

/**   
 * @ClassName:  ResponseResult   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: lcp
 * @date:   2020年7月24日 下午4:41:46   
 *     
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved. 
 */
public class ResponseResult {
	// 定义jackson对象
		private static final ObjectMapper MAPPER = new ObjectMapper();

		// 响应业务状态
		private Integer code;
		// 数据长度
		private Integer count;

		// 响应消息
		private String msg;

		// 响应中的数据
		private Object data;

		public static ResponseResult build(ResultCode resultCode, Object data, int count) {
			return new ResponseResult(resultCode,data, count);
		}

		public ResponseResult() {

		}

		public ResponseResult(ResultCode resultCode, Object data) {
			this.code = resultCode.getCode();
			this.msg = resultCode.getMsg();
			this.data = data;
		}

		public ResponseResult(ResultCode resultCode, Object data, int count) {
			this.code = resultCode.getCode();
			this.msg = resultCode.getMsg();
			this.data = data;
			this.count = count;
		}

		public ResponseResult(Object data) {
			this.code = 200;
			this.msg = "OK";
			this.data = data;
		}

		/**
		 * @param code
		 * @param msg
		 */
		public ResponseResult(int code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		public Integer getCode() {
			return code;
		}

		public void setCode(Integer code) {
			this.code = code;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		public Object getData() {
			return data;
		}

		public void setData(Object data) {
			this.data = data;
		}

		/**
		 * 没有object对象的转化
		 * 
		 * @param json
		 * @return
		 */
		public static ResponseResult format(String json) {
			try {
				return MAPPER.readValue(json, ResponseResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		public String toString() {
			return "ResponseResult [status=" + code + ", msg=" + msg + ", data=" + data + "]";
		}

		public Integer getCount() {
			return count;
		}

		public void setCount(Integer count) {
			this.count = count;
		}

		/**   
		 * @Title: success   
		 * @Description: TODO(这里用一句话描述这个方法的作用)   
		 * @param: @return      
		 * @return: ResponseResult      
		 * @throws   
		 */
		public static ResponseResult success() {
			return new ResponseResult(200,"OK");
		}

	}
